package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jiyun.entity.RoleMenu;
import com.jiyun.entity.UserRole;
import com.jiyun.service.IRoleMenuService;
import com.jiyun.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.jiyun.entity.Role;
import com.jiyun.service.IRoleService;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-05-04
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleService roleService;

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addRole(@RequestBody Role role){
        roleService.save(role);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateRole(@RequestBody Role role){
        roleService.updateById(role);
    }
    @Autowired
    IUserRoleService iUserRoleService;
    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public String deleteOne(@RequestParam Integer id){
        //判断是否存在关联数据
        LambdaQueryWrapper<UserRole> userRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRoleLambdaQueryWrapper.eq(UserRole::getRid,id);
        int count = iUserRoleService.count(userRoleLambdaQueryWrapper);
        if (count>0){
            return "存在关联数据不能删除";
        }
        roleService.removeById(id);
        return "删除成功";
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        roleService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public Role findById(@RequestParam Integer id){
        return roleService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<Role> findAll(){
        return roleService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<Role> findPage(@RequestParam Integer page,@RequestParam Integer pageSize,@RequestBody Role role){
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleLambdaQueryWrapper.like(StringUtils.isNotBlank(role.getRname()),Role::getRname,role.getRname());
        Page<Role> rolePage = new Page<>(page, pageSize);
        return roleService.page(rolePage,roleLambdaQueryWrapper);
    }
    @Autowired
    IRoleMenuService iRoleMenuService;
    @RequestMapping("findRoleMenu")
    public List<RoleMenu> findRoleMenu(@RequestParam Integer id){
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(RoleMenu::getRid,id);
        List<RoleMenu> list = iRoleMenuService.list(roleMenuLambdaQueryWrapper);
        return list;
    }

    @RequestMapping("updateRoleMenu")
    public void updateRole(@RequestParam Integer rid,@RequestParam String mids){
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(RoleMenu::getRid,rid);
        iRoleMenuService.remove(roleMenuLambdaQueryWrapper);
        String[] split = mids.split(",");
        for (String s : split) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRid(rid);
            roleMenu.setMid(Integer.parseInt(s));
            iRoleMenuService.save(roleMenu);
        }

    }

}
